from PyQt5.QtWidgets import *
from PyQt5.QtGui import*#界面常用组件
from PyQt5.QtCore import *
from PyQt5 import*
from MyButton import MyButton
from Chess import Chess
import time
import copy
import  sys
import cgitb
cgitb.enable(format="text")
class DoublePlayGame(QWidget):

    backSignal=pyqtSignal()

    posWhite=[]
    posBlack=[]

    chessArr=[]
    oldChessArr=[]

    isLabel=False

    def __init__(self,parent=None):
        super().__init__(parent)

        self.isBlack=True

        self.resize(760,650)
        self.setWindowTitle("双人对战")

        #设置背景图片
        p=QPalette(self.palette())      #调色板
        brush=QBrush(QImage("./source/游戏界面.png"))
        p.setBrush(QPalette.Background,brush)
        self.setPalette(p)

        #设置图标
        self.setWindowIcon(QIcon("./source/icon.ico"))
        #设置固定大小
        self.setFixedSize(760,650)
        #添加按钮
        self.button1=MyButton("./source/返回按钮_hover.png","./source/返回按钮_press.png","./source/返回按钮_normal.png",parent=self)
        self.button1.move(650,50)
        self.button2=MyButton("./source/开始按钮_hover.png","./source/开始按钮_press.png","./source/开始按钮_normal.png",parent=self)
        self.button2.move(650,200)
        self.button3=MyButton("./source/悔棋按钮_hover.png","./source/悔棋按钮_press.png","./source/悔棋按钮_normal.png",parent=self)
        self.button3.move(650,250)
        self.button4=MyButton("./source/认输按钮_hover.png","./source/认输按钮_press.png","./source/认输按钮_normal.png",parent=self)
        self.button4.move(650,300)

        #设置初始化界面不可点击

        #绑定返回按钮和发送返回信号
        self.button1.clicked.connect(self.goBack)
        #绑定开始按钮和重绘函数
        self.button2.clicked.connect(self.newPaint)
        #悔棋槽函数和信号绑定
        self.button3.clicked.connect(self.backChess)
        #认输按钮
        self.button4.clicked.connect(self.giveup)

    #新界面重新开始
    def newPaint(self):
        if self.isLabel:
            self.labelWin.close()
        self.isBlack=True
        if self.isWin("black") or self.isWin("white"):
            self.labelWin.close()
        self.oldChessArr.clear()
        for i in self.chessArr:
            self.oldChessArr.append(i)
        self.posBlack.clear()
        self.posWhite.clear()
        self.chessArr.clear()
        if len(self.oldChessArr)!=0:
            for c in self.oldChessArr:
                c.hide()

    #悔棋
    def backChess(self):
        if self.isLabel:
            self.labelWin.close()
        if self.isWin("black") or self.isWin("white"):
            self.labelWin.close()
        self.oldChessArr.clear()
        for i in self.chessArr:
            self.oldChessArr.append(i)
        if len(self.posBlack)>len(self.posWhite):
            self.isBlack=False
        elif len(self.posBlack)<len(self.posWhite):
            self.isBlack=True
        #白子想悔棋
        if self.isBlack:
            if len(self.posWhite)!=0:
                self.posWhite.pop()
            if len(self.chessArr)!=0:
                self.chessArr.pop()
            print("白子悔棋后：",self.posWhite)
            print("Chess",self.chessArr)
            print("oldChess：",self.oldChessArr)
            self.repaint()
            self.isBlack=False
        #黑子想悔棋
        else:
            if len(self.posBlack)!=0:
                self.posBlack.pop()
            if len(self.chessArr)!=0:
                self.chessArr.pop()
            print("黑子悔棋后：",self.posBlack)
            print("Chess",self.chessArr)
            print("oldChess：",self.oldChessArr)
            self.repaint()
            self.isBlack=True

    #重写重绘界面函数
    def repaint(self):
        if self.isLabel:
            self.labelWin.close()
        self.chessArr.clear()
        if len(self.oldChessArr)!=0:
            for c in self.oldChessArr:
                c.hide()
        for i in range(0,19):
            for j in range(0,19):
                _x=50+i*self.width
                _y=50+j*self.width
                if _x*19+_y in self.posBlack:
                    self.chess=Chess(True,parent=self)
                    self.chess.move(_x-15,_y-15)
                    self.chess.show()
                    self.chessArr.append(self.chess)
                if _x*19+_y in self.posWhite:
                    self.chess=Chess(False,parent=self)
                    self.chess.move(_x-15,_y-15)
                    self.chess.show()
                    self.chessArr.append(self.chess)

    #发送返回信号
    def goBack(self):
        self.backSignal.emit()

    def mouseReleaseEvent(self, a0: QtGui.QMouseEvent):
        self.chess=Chess(self.isBlack,parent=self)
        #左上角坐标50,50  右下角坐标590,590
        self.width=(590-50)/18 #每个格子大小30.0
        #print(self.width)
        x=int(a0.x())
        y=int(a0.y())
        if x>=35 and y>=35 and x<=605 and y<=605:
            for i in range(0,19):
                for j in range(0,19):
                    _x=50+i*self.width
                    _y=50+j*self.width
                    if abs(x-_x)<=15 and abs(y-_y)<=15 and _x*19+_y not in self.posBlack and _x*19+_y not in self.posWhite and not self.isWin("black") and not self.isWin("white"):
                        self.chess.move(_x-15,_y-15)
                        self.chess.show()
                        if self.isBlack:
                            if _x*19+_y not in self.posBlack:
                                self.posBlack.append(_x*19+_y)
                                self.chessArr.append(self.chess)
                            if self.isWin("black"):
                                print("黑子赢了!")
                                self.showWinPic("black")
                                self.isBlack=False
                                return
                            self.isBlack=False
                        else:
                            if _x*19+_y not in self.posWhite:
                                self.posWhite.append(_x*19+_y)
                                self.chessArr.append(self.chess)
                            if self.isWin("white"):
                                print("白子赢了")
                                self.showWinPic("white")
                                self.isBlack=True
                                return
                            self.isBlack=True
        print("黑子：",self.posBlack)
        print("白子：",self.posWhite)

    #胜利显示图片
    def showWinPic(self,color):
        if color=="black":
            self.pixmapWin=QPixmap("./source/黑棋胜利.png")
            self.labelWin=QLabel(parent=self)
            self.labelWin.setPixmap(self.pixmapWin)
            self.labelWin.setFixedSize(self.pixmapWin.size())
            self.labelWin.show()
            self.labelWin.move(50,50)
        else:
            self.pixmapWin=QPixmap("./source/白棋胜利.png")
            self.labelWin=QLabel(parent=self)
            self.labelWin.setPixmap(self.pixmapWin)
            self.labelWin.setFixedSize(self.pixmapWin.size())
            self.labelWin.show()
            self.labelWin.move(50,50)
        self.isLabel=True

    #自己认输显示图片
    def giveup(self):
        if self.isWin("black") or self.isWin("white"):
            self.labelWin.close()
        #黑子认输
        if self.isBlack:
            self.pixmapWin=QPixmap("./source/白棋胜利.png")
            self.labelWin=QLabel(parent=self)
            self.labelWin.setPixmap(self.pixmapWin)
            self.labelWin.setFixedSize(self.pixmapWin.size())
            self.labelWin.show()
            self.labelWin.move(50,50)

        #白子认输
        else:
            self.pixmapWin=QPixmap("./source/黑棋胜利.png")
            self.labelWin=QLabel(parent=self)
            self.labelWin.setPixmap(self.pixmapWin)
            self.labelWin.setFixedSize(self.pixmapWin.size())
            self.labelWin.show()
            self.labelWin.move(50,50)
        self.isLabel=True

    #判断输赢
    def isWin(self,color):
        if color=="black":
            #判断每行是否有五黑子连在一起
            for i in range(0,15):
                for j in range(0,19):
                    _x=50+i*self.width
                    _y=50+j*self.width
                    if _x*19+_y in self.posBlack and (_x+30)*19+_y in self.posBlack and (_x+60)*19+_y in self.posBlack and (_x+90)*19+_y in self.posBlack and (_x+120)*19+_y in self.posBlack:
                        return True
            #判断列
            for j in range(0,15):
                for i in range(0,19):
                    _x=50+i*self.width
                    _y=50+j*self.width
                    if _x*19+_y in self.posBlack and _x*19+30+_y in self.posBlack and _x*19+_y+60 in self.posBlack and _x*19+_y+90 in self.posBlack and _x*19+_y+120 in self.posBlack:
                        return True
            #判断左斜
            for i in range(0,15):
                for j in range(0,15):
                    _x=50+i*self.width
                    _y=50+j*self.width
                    if _x*19+_y in self.posBlack and (_x+30)*19+30+_y in self.posBlack and (_x+60)*19+_y+60 in self.posBlack and (_x+90)*19+_y+90 in self.posBlack and (_x+120)*19+_y+120 in self.posBlack:
                        return True
            #判断右斜
            for i in range(0,15):
                for j in range(0,19):
                    _x=50+i*self.width
                    _y=50+j*self.width
                    if _x*19+_y in self.posBlack and (_x+30)*19-30+_y in self.posBlack and (_x+60)*19+_y-60 in self.posBlack and (_x+90)*19+_y-90 in self.posBlack and (_x+120)*19+_y-120 in self.posBlack:
                        return True
            return False
        else:
            #判断每行是否有五白子连在一起
            for i in range(0,15):
                for j in range(0,19):
                    _x=50+i*self.width
                    _y=50+j*self.width
                    if _x*19+_y in self.posWhite and (_x+30)*19+_y in self.posWhite and (_x+60)*19+_y in self.posWhite and (_x+90)*19+_y in self.posWhite and (_x+120)*19+_y in self.posWhite:
                        return True
            #判断列
            for j in range(0,15):
                for i in range(0,19):
                    _x=50+i*self.width
                    _y=50+j*self.width
                    if _x*19+_y in self.posWhite and _x*19+30+_y in self.posWhite and _x*19+_y+60 in self.posWhite and _x*19+_y+90 in self.posWhite and _x*19+_y+120 in self.posWhite:
                        return True
            #判断左斜
            for i in range(0,15):
                for j in range(0,15):
                    _x=50+i*self.width
                    _y=50+j*self.width
                    if _x*19+_y in self.posWhite and (_x+30)*19+30+_y in self.posWhite and (_x+60)*19+_y+60 in self.posWhite and (_x+90)*19+_y+90 in self.posWhite and (_x+120)*19+_y+120 in self.posWhite:
                        return True
            #判断右斜
            for i in range(0,15):
                for j in range(0,19):
                    _x=50+i*self.width
                    _y=50+j*self.width
                    if _x*19+_y in self.posWhite and (_x+30)*19-30+_y in self.posWhite and (_x+60)*19+_y-60 in self.posWhite and (_x+90)*19+_y-90 in self.posWhite and (_x+120)*19+_y-120 in self.posWhite:
                        return True
            return False

'''
if __name__=="__main__":
    a=QApplication(sys.argv)
    m=DoublePlayGame()
    m.show()
    sys.exit(a.exec_())
'''